package drive

import (
	"game/util"
	"net"
	"strconv"
)

type SocketStruct struct {
	Host string //IP地址
	Port int //端口号
	Listener net.Listener //Listener 端口监听
	Conn []net.Conn
}

var SocketConn = make(map[int]SocketStruct)

func AddSocketListen(Port int,Host string)  {
	if _,ok:=SocketConn[Port];ok {
		util.Instance().PanicFmt("Socket已经存在!")
	}
	listen,err :=net.Listen("tcp",Host+":"+strconv.Itoa(Port))
	if err!=nil {
		util.Instance().PanicFmt("Socket链接错误:",err)
	}
	mkConn := make([]net.Conn,0)
	SocketConn[Port] = SocketStruct{Host,Port,listen,mkConn}
}

func GetSocketListen(Port int) SocketStruct {
	if _,ok:=SocketConn[Port];!ok {
		util.Instance().PanicFmt("Socket不存在")
	}
	return SocketConn[Port]
}